package top.lywivan.netty.reactor;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Properties;

public class ServerConfig {

    protected InetSocketAddress clientPortAddress ;


    /** defaults to -1 if not set explicitly */
    protected int clientPortListenBacklog = -1;

    protected int maxClientCnxns = 60;

    protected static final String defaultConfigFile = "server.conf";

    protected String configFilePath = null;


    public void parse() throws ConfigException{
        InputStream is = ServerConfig.class.getClassLoader().getResourceAsStream(defaultConfigFile);
        parse(is);
    }

    public void parse(InputStream ins) throws ConfigException{
        try {
            // 读入properties
            Properties configProp = new Properties();
            try {
                configProp.load(ins);
            } finally {
                ins.close();
            }
            parseProperties(configProp);
        }catch (Exception e) {
            throw new ConfigException(e.getMessage());
        }
    }


    public void parse(String configPath) throws ConfigException{
        File configFile = new File(configPath);
        InputStream configInputStream = null;
        try {
            configInputStream = new FileInputStream(configFile);
        } catch (FileNotFoundException e) {
            throw new ConfigException("FileNotFound");
        }
        parse(configInputStream);
    }

    private void parseProperties(Properties prop) throws IOException {
        int port = 0;
        String address = null;

        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
            String key = entry.getKey().toString().trim();
            String value = entry.getValue().toString().trim();
            if (key.equals("port")) {
                port = Integer.parseInt(value);
            }else if (key.equals("address")) {
                address = value;
            }else if (key.equals("clientPortListenBacklog")) {
                clientPortListenBacklog = Integer.parseInt(value);
            }else if (key.equals("maxClientCnxns")) {
                maxClientCnxns = Integer.parseInt(value);
            }else {

            }
        }

        if (port == 0) {
            if (clientPortAddress != null) {
                throw new IllegalArgumentException("port is not set");
            }
        }else if (address !=null) {
            clientPortAddress = new InetSocketAddress(InetAddress.getByName(address),port);
        }else {
            clientPortAddress = new InetSocketAddress(port);
        }


    }


    public void parse(String[] args) {
        /*if (args.length < 2 || args.length > 4) {
            throw new IllegalArgumentException("Invalid number of arguments:" + Arrays.toString(args));
        }
        clientPortAddress = new InetSocketAddress(Integer.parseInt(args[0]));*/
    }

    public InetSocketAddress getClientPortAddress() {
        return clientPortAddress;
    }

    public int getClientPortListenBacklog() {
        return clientPortListenBacklog;
    }

    public int getMaxClientCnxns() {
        return maxClientCnxns;
    }

    public String getConfigFilePath() {
        return configFilePath;
    }




    public static class ConfigException extends Exception {

        public ConfigException(String msg) {
            super(msg);
        }
        public ConfigException(String msg, Exception e) {
            super(msg, e);
        }

    }

}
